using namespace std;
#include<iostream>
#include<string>
#include<set>
#include<vector>
#include<queue>


struct TreeNode {
        int val;
        TreeNode *left;
        TreeNode *right;
        TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    };

class Solution {
public:

    vector<int> levelOrder(TreeNode* root) {
        vector<int> ans;
        queue<TreeNode*> q;
        if(root == nullptr) return ans;
        q.push(root);
        while (!q.empty()){
            TreeNode* temp = q.front();
            q.pop();
            ans.push_back(temp->val);
            if(temp->left!=nullptr){
                q.push(temp->left);
            }
            if(temp->right!=nullptr){
                q.push(temp->right);
            }
        }
        return ans;
    }

    vector<vector<int>> levelOrder2(TreeNode* root) {
        vector<vector<int>> ans;
        queue<TreeNode*> q;
        if(root == nullptr) return ans;
        q.push(root);
        while (!q.empty()){
            int n = q.size();
            vector<int> level_data;
            for (int i = 0; i < n; ++i) {
                TreeNode* temp = q.front();
                q.pop();
                level_data.push_back(temp->val);
                if(temp->left!=nullptr){
                    q.push(temp->left);
                }
                if(temp->right!=nullptr){
                    q.push(temp->right);
                }
                if(ans.size()%2==1) reverse(level_data.begin(),level_data.end());
            }ans.push_back(level_data);
        }
        return ans;
    }

};

int main(){
    Solution a;
}